//package cn.xie.blog.servergateway.filler;
//
//import cn.hutool.extra.spring.SpringUtil;
//import cn.xie.blog.serverutil.util.JwtUtil;
//import cn.xie.blog.serverutil.constant.jwt.JwtContants;
//import cn.xie.blog.serverutil.util.RedisUtil;
//import jakarta.annotation.Resource;
//import jakarta.servlet.FilterChain;
//import jakarta.servlet.ServletException;
//import jakarta.servlet.http.HttpServletRequest;
//import jakarta.servlet.http.HttpServletResponse;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.cloud.gateway.filter.GatewayFilterChain;
//import org.springframework.cloud.gateway.filter.GlobalFilter;
//import org.springframework.core.Ordered;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.server.reactive.ServerHttpRequest;
//import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
//import org.springframework.security.core.context.SecurityContextHolder;
//import org.springframework.security.core.userdetails.UserDetails;
//import org.springframework.security.core.userdetails.UserDetailsService;
//import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
//import org.springframework.stereotype.Component;
//import org.springframework.util.StringUtils;
//import org.springframework.web.filter.OncePerRequestFilter;
//import org.springframework.web.server.ServerWebExchange;
//import reactor.core.publisher.Mono;
//
//import java.io.IOException;
//
///**
// * @description: 令牌检查过滤器
// * @author: xiaoxie
// * @date: 2025/4/30 上午11:13
// **/
//@Component
//@Slf4j
//public class CheckTokenFilter extends OncePerRequestFilter{
//
//    @Resource
//    private JwtUtil jwtUtil;
//    @Resource
//    private  UserDetailsService userDetailsService;
//
//    @Override
//    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
//        log.info("开始执行令牌检查过滤器");
//        String header = request.getHeader("Authorization");
//        log.debug("从请求头中获取到的令牌：{}", header);
//        // 如果没有令牌，跳过该过滤器
//        if (StringUtils.hasText(header) && header.startsWith("Bearer ")) {
//            String token = header.substring(7);
//            log.debug("解析出的令牌：{}", token);
//
//            if (StringUtils.hasText(token)) {
//                try {
//                    // 从令牌中解析用户名
//                    String username = jwtUtil.parseClaims(token).get(JwtContants.CLAIM_KEY_USER, String.class);
//                    log.debug("从令牌中解析出的用户名：{}", username);
//
//                    Object cacheObject = SpringUtil.getBean(RedisUtil.class).get(JwtContants.TOKEN_PREFIX + username);
//                    log.debug("从Redis获取的缓存令牌：{}", cacheObject);
//
//                    if (cacheObject != null && cacheObject.equals(token)) {
//                        // 加载用户信息
//                        UserDetails user = userDetailsService.loadUserByUsername(username);
//                        log.debug("从数据库加载的用户信息：{}", user.getUsername());
//
//                        // 验证token是否有效
//                        if (jwtUtil.validateToken(token, user)) {
//                            log.info("令牌验证通过");
//
//                            // 生成新的令牌并设置新的过期时间
//                            String newToken = jwtUtil.getUserNameFromToken(user.getUsername());
//                            response.setHeader("New-Token", newToken);
//                            log.debug("生成的新令牌：{}", newToken);
//
//                            // 更新Redis中令牌的过期时间
//                            SpringUtil.getBean(RedisUtil.class).expire(JwtContants.TOKEN_PREFIX + username, JwtContants.getExpireDate().getTime());
//                            log.debug("更新Redis中令牌的过期时间：{}", JwtContants.getExpireDate().getTime());
//
//                            // 创建认证信息
//                            UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(
//                                    user, null, user.getAuthorities()
//                            );
//                            authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
//                            log.debug("创建的认证信息：{}", authenticationToken);
//
//                            // 设置到Spring Security上下文
//                            SecurityContextHolder.getContext().setAuthentication(authenticationToken);
//                            log.info("将认证信息设置到Spring Security上下文");
//                        }
//                    }
//                } catch (Exception e) {
//                    // 处理token解析或验证失败的情况
//                    log.error("令牌解析或验证失败：{}", e.getMessage(), e);
//                }
////            }
//            }
//        log.info("继续过滤链执行");
//        filterChain.doFilter(request, response);
//    }
//
//}